﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class UIWFN : ViewBase
    {

        public object EventTag
        {
            get;
            set;
        }
        public UIWFN()
        {

        }
        public virtual void OnMSGTYPChg(string mtyp)
        {
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            IsERL = false;
            if (TMV.CONTROL.Trim() == "OK")
            {
                IsERL = true;
            }
        }
        public override void New()
        {

            if (EventTag == null)
                return;

            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            EventTag = null;
            Insert(dwData);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {

            PM["CUSER"] = app.User;
            PM["EVENTID"] =TMV.ART;
            PM["dwInput"] = null;
            PM["KRS"] = TMV.EXAUF.Substring(0, 2);
            PM["LFD"] = TMV.EXAUF.Substring(2, 6);
            PM["POS"] = TMV.EXAUF.Substring(8, 3);
            PM["MSG"] = new MessageInfo();
            PM["INFO"] = TMV.DES;
            VICC_WFEVENTS.SVWF.NEWEVENTS(PM);
            IsWFApplied = false;
            IsWFApplied = true;
            base.NewCommand(sender, e);
        }
        public override void OnEXAUFChg(string vlaue)
        {
            if (EXAUF == "")
                return;
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 6);
            POS = EXAUF.Substring(8, 3);
            base.OnEXAUFChg(vlaue);
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                dwData = TBMain.NewRow();
                dwData["MANDANT"] = app.Mandant;
                dwData["KRS"] = KRS;
                dwData["LFD"] = LFD;
                dwData["POS"] = POS;
                SResult = TMV.QWF06(dwData);
                MergeData(SResult, false, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Insert(DataRow _dwWF)
        {
            TMV.InsertICC_WFEVENTS(_dwWF, app.company);
            TBMain.Rows.InsertAt(_dwWF, 0);
            vwMaster.View.MoveCurrentToFirst();
        }
        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
        }
        public void CheckEvents(string events, DataRow dwInput, string msg = "", MessageInfo msgs = null)
        {
            TMV.CheckEvents(events, dwInput, msg, msgs);
        }
        public override void EditCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            base.EditCommand(sender, e);
        }
        public void ApproveCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                if (app.User.Trim() != TMV.WFEUSER.Trim())
                    throw new Exception("你不是他");
                dwMaster["CONTROL"] = "OK ";
                dwMaster["WFWHEN"] = System.DateTime.Now.ToString();
                TMV.UpdateICC_WFEVENTS(dwMaster);
                if (dwMaster["NEXTEVENTS1"].ToString().Trim().Length > 2)
                {
                    PM["CUSER"] = app.User;
                    PM["EVENTID"] = dwMaster["NEXTEVENTS1"].ToString();
                    PM["dwInput"] = null;
                    PM["KRS"] = dwMaster["KRS"].ToString();
                    PM["LFD"] = dwMaster["LFD"].ToString();
                    PM["POS"] = dwMaster["POS"].ToString();
                    PM["INFO"] = dwMaster["P1"].ToString();
                    PM["DEFCONTROL"] = dwMaster["DEFCONTROL1"].ToString().PadRight(3);
                    PM["MSG"] = new MessageInfo();
                    TMV.NEWEVENTS(PM);
                }
                IsWFApproved = false;
                IsWFApproved = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void RejectCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                if (app.User.Trim() != TMV.WFEUSER.Trim())
                    throw new Exception("你不是他");
                dwMaster["CONTROL"] = "NO ";
                dwMaster["WFWHEN"] = System.DateTime.Now.ToString();
                TMV.UpdateICC_WFEVENTS(dwMaster);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
    public partial class UIWFN : ViewBase
    {
        public VICC_WFEVENTS TMV
        {
            get
            {
                return (VICC_WFEVENTS)MV;
            }
        }
        private VICC_WFEVENTS _VWF;
        public VICC_WFEVENTS VWF
        {
            get
            {
                if (_VWF == null) _VWF = new VICC_WFEVENTS();
                return _VWF;
            }
        }
        #region ==CUSER===========
        public static readonly DependencyProperty CUSERProperty = DependencyProperty.Register("CUSER",
            typeof(string), typeof(UIWFN),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCUSERChanged));
        public string CUSER
        {
            get
            {
                return (string)GetValue(CUSERProperty);
            }
            set
            {
                SetValue(CUSERProperty, value);
                OnCUSERChg(value);
            }
        }
        private static void OnCUSERChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWFN;
            if (src != null)
            {
                src.CUSER = (string)e.NewValue;
            }
        }
        public virtual void OnCUSERChg(string user)
        {
            if (!IsCanLoad)
                return;
            dwData = TBMain.NewRow();
            dwData["MANDANT"] = app.Mandant;
            dwData["CONTROL"] = CONTROL.PadRight(3);
            dwData["WFEUSER"] = user;
            SResult = TMV.QWF0501(dwData);
            MergeData(SResult, false, true);
        }
        #endregion 
        #region ==CONTROL===========
        public static readonly DependencyProperty CONTROLProperty = DependencyProperty.Register("CONTROL",
            typeof(string), typeof(UIWFN),
            new FrameworkPropertyMetadata("   ", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCONTROLChanged));
        public string CONTROL
        {
            get
            {
                return (string)GetValue(CONTROLProperty);
            }
            set
            {
                SetValue(CONTROLProperty, value);
                OnCONTROLChg(value);
            }
        }
        private static void OnCONTROLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWFN;
            if (src != null)
            {
                src.CONTROL = (string)e.NewValue;
            }
        }
        public virtual void OnCONTROLChg(string control)
        {
            if (!IsCanLoad)
                return;
            dwData = TBMain.NewRow();
            dwData["MANDANT"] = app.Mandant;
            dwData["CONTROL"] = control.PadRight(3);
            dwData["WFEUSER"] = CUSER;
            SResult = TMV.QWF0501(dwData);
            MergeData(SResult, false, true);
        }
        #endregion 
        #region ==MSGTYP===========
        public static readonly DependencyProperty MSGTYPProperty = DependencyProperty.Register("MSGTYP",
            typeof(string), typeof(UIWFN),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMSGTYPChanged));
        public string MSGTYP
        {
            get
            {
                return (string)GetValue(MSGTYPProperty);
            }
            set
            {
                SetValue(MSGTYPProperty, value);
                OnMSGTYPChg(value);
            }
        }
        private static void OnMSGTYPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWFN;
            if (src != null)
            {
                src.MSGTYP = (string)e.NewValue;
            }
        }
        #endregion

        #region ==IsInfo===========
        public static readonly DependencyProperty IsInfoProperty = DependencyProperty.Register("IsInfo",
            typeof(bool), typeof(UIWFN),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsInfoChanged));
        public bool IsInfo
        {
            get
            {
                return (bool)GetValue(IsInfoProperty);
            }
            set
            {
                SetValue(IsInfoProperty, value);
            }
        }
        private static void OnIsInfoChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWFN;
            if (src != null)
            {
                src.IsInfo = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsERL===========
        public static readonly DependencyProperty IsERLProperty = DependencyProperty.Register("IsERL",
            typeof(bool), typeof(UIWFN),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsERLChanged));
        public bool IsERL
        {
            get
            {
                return (bool)GetValue(IsERLProperty);
            }
            set
            {
                SetValue(IsERLProperty, value);
            }
        }
        private static void OnIsERLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWFN;
            if (src != null)
            {
                src.IsERL = (bool)e.NewValue;
            }
        }
        #endregion

     

        #region ==DLL===========
        public static readonly DependencyProperty DLLProperty = DependencyProperty.Register("DLL",
            typeof(string), typeof(UIWFN),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDLLChanged));
        public string DLL
        {
            get
            {
                return (string)GetValue(DLLProperty);
            }
            set
            {
                SetValue(DLLProperty, value);
                OnDLLChg();
            }
        }
        private static void OnDLLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWFN;
            if (src != null)
            {
                src.DLL = (string)e.NewValue;
            }
        }
        public virtual void OnDLLChg()
        {

        }
        #endregion
        #region ==CMD===========
        public static readonly DependencyProperty CMDProperty = DependencyProperty.Register("CMD",
            typeof(string), typeof(UIWFN),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCMDChanged));
        public string CMD
        {
            get
            {
                return (string)GetValue(CMDProperty);
            }
            set
            {
                SetValue(CMDProperty, value);
                OnCMDChg();
            }
        }
        private static void OnCMDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWFN;
            if (src != null)
            {
                src.CMD = (string)e.NewValue;
            }
        }
        public virtual void OnCMDChg()
        {

        }
        #endregion 

        #region ==STWNW===========
        public static readonly DependencyProperty STWNWProperty = DependencyProperty.Register("STWNW",
            typeof(string), typeof(UIWFN),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSTWNWChanged));
        public string STWNW
        {
            get
            {
                return (string)GetValue(STWNWProperty);
            }
            set
            {
                SetValue(STWNWProperty, value);
                OnSTWNWChg();
            }
        }
        private static void OnSTWNWChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWFN;
            if (src != null)
            {
                src.STWNW = (string)e.NewValue;
            }
        }
        public virtual void OnSTWNWChg()
        {

        }
        #endregion
    }
}
